﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Qreed.LoremIpsum
{
    /// <summary>
    /// Generator options flags.
    /// </summary>
    [Flags]
    public enum GeneratorOptions
    {
        /// <summary>
        /// No options
        /// </summary>
        None,
        /// <summary>
        /// Capitalize the first word.
        /// </summary>
        CapitalizeLines,
        /// <summary>
        /// Append point to the end of the line.
        /// </summary>
        AppendLinePoints
    }

    /// <summary>
    /// A very crude lorem ipsum text generator
    /// </summary>
    public static class Generator
    {
        private const string LoremIpsumText = "Lorem ipsum dolor sit amet consectetur adipiscing elit Curabitur posuere sem neque Quisque eu lacus est ac semper lorem Pellentesque vel felis risus in ultricies magna Fusce eleifend elementum elementum Curabitur id justo tortor Donec at libero metus non commodo neque Aliquam et orci nibh Aenean placerat eleifend massa ut aliquet leo hendrerit ac Morbi vitae turpis id sem euismod congue Donec aliquam fermentum lorem ut lacinia Etiam quis velit diam quis consectetur arcu Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras id pellentesque urna Nulla dignissim erat id erat luctus egestas Quisque vulputate sodales turpis sit amet varius lectus imperdiet sed Proin tellus lectus commodo quis interdum eget adipiscing consequat lorem Suspendisse bibendum auctor lacinia Duis mattis felis quis volutpat accumsan ligula nulla bibendum nunc vitae tincidunt sapien ligula sollicitudin quam Duis sit amet mi id neque lacinia tempor vitae venenatis enim";

        private static string[] _words = null;
        private static Random _rnd = new Random();

        static Generator()
        {
            _words = LoremIpsumText.ToLower().Split(" ".ToCharArray());
        }

        /// <summary>
        /// Creates a random lorem ipsum line.
        /// </summary>
        /// <param name="minWordCount">The minimum word count.</param>
        /// <param name="maxWordCount">The maximum word count.</param>
        /// <param name="options">The options.</param>
        /// <returns>The random lorem ipsum text.</returns>
        public static string CreateLine(int minWordCount, int maxWordCount, GeneratorOptions options)
        {
            int wordCount = _rnd.Next(minWordCount, maxWordCount);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < wordCount; i++)
            {
                int index = _rnd.Next(_words.Length);

                sb.Append(_words[index]);
                sb.Append(" ");
            }

            sb.Length -= 1; // Remove the last " "

            if ((options & GeneratorOptions.CapitalizeLines) == GeneratorOptions.CapitalizeLines)
            {
                sb.Replace(sb[0], Char.ToUpper(sb[0]), 0, 1);
            }

            if ((options & GeneratorOptions.AppendLinePoints) == GeneratorOptions.AppendLinePoints)
                sb.Append(".");

            return sb.ToString();
        }
    }
}
